home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14121 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  38 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!smryan
  3. From: smryan@netcom.com (@#$%!?!)
  4. Subject: Re: About union
  5. Message-ID: <smryanDpqCB7.F0w@netcom.com>
  6. Organization: The Programmer formerly known as S M Ryan
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <4kfj5g$24n@dewey.csun.edu>
  9. Date: Fri, 12 Apr 1996 03:28:19 GMT
  10. Sender: smryan@netcom21.netcom.com
  11.  
  12. :   (void) printf("The value of integer is %d\n",     myUnion.integer);
  13. :   (void) printf("The value of floating is %f\n\n",  myUnion.floating);
  14.  
  15. The current bits in the union are interpreted according to the type of
  16. the member you use. myUnion.integer interprets the bits as an int, 
  17. myUnion.floating interprets the exact same bit pattern as a float. The
  18. compiler make no attempt coerce the bits or verify the union was assigned
  19. and accessed with the same types.
  20.  
  21. :   (void) printf("The value of \"cast\" is %f\n",    (float) myUnion.integer);
  22.  
  23. Integer coerced to float as expected.
  24.  
  25. :   (void) printf("The value of \"magic\" is %f\n\n", myUnion.integer);
  26.  
  27. printf expects a double parameter with an %f descriptor.
  28.  
  29. :   (void) printf("The meaning of life ``%d''\n",     (int) myUnion.floating);
  30.  
  31. The bits are interpreted as a float and then coerced to int.
  32.  
  33. -- 
  34. The Queen, amused, in quiet power,         | smryan@netcom.com  PO Box 1563
  35. will draw the son to darkenned bower.      |          Cupertino, California
  36. Her face is fair, her fragrance rare,      | (xxx)xxx-xxxx            95015
  37. with woven webs for wayward flower.        |         I don't use no smileys
  38.